﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace RG
{
    public partial class WinForm : Form
    {
        public WinForm() => InitializeComponent(); private Random Rand = new Random();
        private readonly Dictionary<Char, Char[]> DM = new Dictionary<Char, Char[]>
        {
            { 'x', new Char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' } },
            { 'X', new Char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' } },
            { '8', new Char[] { '2', '3', '4', '5', '6', '7', '8', '9' } },
            { '@', new Char[] { '~', '!', '@', '#', '$', '%', '&', '*', '-', '_', '=', '+', ',', '.', ':' } },
            { '?', new Char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '2', '3', '4', '5', '6', '7', '8', '9', '~', '!', '@', '#', '$', '%', '&', '*', '-', '_', '=', '+', '|', ',', '.', ':' } },
            { 'i', new Char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' } },
            { 'I', new Char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' } },
            { '0', new Char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' } },
            { '$', new Char[] { '`', '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '=', '+', '[', '{', ']', '}', '\x5C', '|', ';', ':', '\x27', '"', ',', '<', '.', '>', '/', '?' } },
            { '*', new Char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '`', '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '=', '+', '[', '{', ']', '}', '\x5C', '|', ';', ':', '\x27', '"', ',', '<', '.', '>', '/', '?' } }
            //   \x5C = \   \x27 = '
        };
        private readonly List<Char> TChars = new List<Char>();
        private void WinMain_Load(object sender, EventArgs e)
        {
            for (Int32 i = 0; i < DateTime.Now.Millisecond; i++) Rand.Next(); Gen();
            foreach (Char K in DM.Keys) TChars.Add(K); TChars.Add((Char)Keys.Back);
        }
        private void R_TextChanged(object s, EventArgs e)
        { String T = ((Label)s).Text.Replace("\r\n", ""); R.Text = T.Length > 32 ? T2(T) : T; }
        private String T2(String S)
        {
            Int32 L = S.Length % 2 == 1 ? S.Length / 2 + 1 : S.Length / 2;
            return $"{S.Substring(0, L)}\r\n{S.Substring(L)}";
        }
        private void Gen()
        {
            StringBuilder B = new StringBuilder(); foreach (Char C in T.Text.ToCharArray())
                B.Append(DM[C][Rand.Next(0, DM[C].Length - 1)]); R.Text = B.ToString();
        }
        private void T_TextChanged(object sender, EventArgs e) => L.Value = T.Text.Length;
        private void G_Click(object sender, EventArgs e) { Gen(); if (AskClipboard) CopyClipboard(); }
        private void UI(object sender, EventArgs e)
        { L.Enabled = !M.Checked; T.Enabled = M.Checked; GT(); Gen(); }
        private void GT()
        {
            Int32 I = (Int32)L.Value; if (N.Checked) T.Text = GN(I);
            if (S.Checked) T.Text = GS(I); if (D.Checked) T.Text = GD(I); if (C.Checked) T.Text = GC(I);
        }
        private String GN(Int32 I) => String.Empty.PadRight(I, '0');
        private String GS(Int32 I)
        { String S = String.Empty.PadRight(I * 2 / 3, 'x'); return S.PadRight(I, '8'); }
        private String GD(Int32 I)
        {
            String S = String.Empty.PadRight(I / 4, 'X'); S = S.PadRight(I / 2, '@');
            S = S.PadRight(I * 3 / 4, 'x'); return S.PadRight(I, '0');
        }
        private String GC(Int32 I)
        {
            switch (I)
            {
                case 1: return "I";
                case 2: return "Ii";
                case 3: return "Ii0";
                case 4: return "Ii0$";
                case 5: return "I*i0$";
                case 6: return "I*i*0$";
                case 7: return "I*i*0*$";
                default:
                    String S = "I"; S = S.PadRight(I / 4, '*'); S += "i"; S = S.PadRight(I / 2, '*');
                    S += "0"; S = S.PadRight(I * 3 / 4, '*'); S += "$"; return S.PadRight(I, '*');
            }
        }
        private void EnterKey(object sender, KeyPressEventArgs e) { if (e.KeyChar == (Char)Keys.Enter) Gen(); }
        private void T_Press(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (Char)Keys.Enter) { Gen(); if (AskClipboard) CopyClipboard(); }
            if (!TChars.Contains(e.KeyChar)) e.Handled = true;
        }
        private void R_DoubleClick(object sender, EventArgs e) { if (AskClipboard) CopyClipboard(); }
        private void CopyClipboard() => Clipboard.SetText(R.Text.Replace("\r\n", ""));
        private Boolean AskClipboard =>
            MessageBox.Show($"已生成结果：\r\n{R.Text}", "已生成", MessageBoxButtons.YesNo,
                MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes;

    }
}
